package com.guochao.myRPCversion3.server;


import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池版服务端的实现
 */
public class ThreadPoolRPCServerImpl implements RPCServer{
    private final ThreadPoolExecutor threadPool;
    private ServiceProvider serviceProvider;

    //默认构造函数：默认设置线程池参数
    public ThreadPoolRPCServerImpl(ServiceProvider serviceProvider) {

        threadPool = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
                1000, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));

        this.serviceProvider = serviceProvider;
    }

    //自定义构造函数：自定义线程池参数
    public ThreadPoolRPCServerImpl(ServiceProvider serviceProvider, int corePoolSize,
                                   int maximumPoolSize,
                                   long keepAliveTime,
                                   TimeUnit unit,
                                   BlockingQueue<Runnable> workQueue) {

        threadPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);

        this.serviceProvider = serviceProvider;
    }

    @Override
    public void start(int port) {
        System.out.println("线程池版服务端启动了");
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            while(true){
                Socket socket = serverSocket.accept();//这里会不断地监听阻塞，一旦有连接请求，就开启一个线程执行
                threadPool.execute(new WorkThread(socket,serviceProvider));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void stop() {

    }
}
